<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品详情</title>
    <link href="../static/css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <link href="../static/css/components.min.css" rel="stylesheet" type="text/css"/>
    <link href="../static/css/login.css" rel="stylesheet" type="text/css"/>
    <script src="../static/js/jquery-1.11.2.min.js" type="text/javascript"></script>
</head>
<body class="login">
    <div class="content">
        <h3 class="form-title">商品详情</h3>
        <div id="promoStartTimeContainer" class="form-group">
            <label style="color:red;" id="promoStatus" class="control-label"></label>
            <div>
                <label style="color:red;" class="control-label" id="promoStartTime" />
            </div>
        </div>
        <div class="form-group">
            <div>
                <label class="control-label" id="title" />
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">商品描述</label>
            <div>
                <label class="control-label" id="description" />
            </div>
        </div>
        <div id="priceContainer" class="form-group">
            <label class="control-label">价格</label>
            <div>
                <label class="control-label" id="price" />
            </div>
        </div>
        <div id="promoPriceContainer" class="form-group">
            <label style="color:red;" class="control-label">秒杀价格</label>
            <div>
                <label style="color:red;" class="control-label" id="promoPrice" />
            </div>
        </div>
        <div class="form-group">
            <div>
                <img id="imgUrl" style="width: 100px;height: auto;">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">库存</label>
            <div>
                <label class="control-label" id="stock" />
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">销量</label>
            <div>
                <label class="control-label" id="sales" />
            </div>
        </div>
        <div class="form-actions">
            <button id="createOrder" class="btn blue" type="submit">下单</button>
        </div>
        
    </div>
</body>

<script>

    var itemVO = {};
    // DOM加载完毕后再执行
    jQuery(document).ready(function(){
        // 绑定OTP的click点击事件，向后端发送请求
        $.ajax({
            type: "GET",
            xhrFields: {
                xhrFields: true
            },
            url: "http://localhost:8888/items/" + getParam("id"),
            success: (res)=>{
                if (res.status == "success") {
                    itemVO = res.data;
                    reloadDom();
                    setInterval(reloadDom,1000);
                } else {
                    alert("获取信息失败，原因为：" + res.data.errorMessage);
                }
            },
            error: (res)=>{
                alert("获取错误，原因为：" + res.responseTesxt)
            }
        });

        $("#createOrder").on("click",function(){
            $.ajax({
                type: "POST",
                contentType: "application/x-www-form-urlencoded;charset=utf-8",
                url: "http://localhost:8888/orders",
                xhrFields: {
                    withCredentials: true
                },
                data: {
                    "itemId": itemVO.id,
                    "amount": 2,
                    "promoId": itemVO.promoId
                },
                success: (res)=>{
                    if (res.status == "success") {
                        alert("下单成功");
                        window.location.reload();
                    } else {
                        alert("下单失败，原因为：" + res.data.errorMessage);
                        if (res.data.errorCode == 20003) {
                            window.location.href="file:///E:/WorkSpace/Seckill/UI/templates/login.html";
                        } 
                    }
                },
                error: (res)=>{
                    alert("下单错误，原因为：" + res.responseTesxt);
                }
            })
        })
    });

    function getParam(paramName){
        paramValue = "", isFound = !1;
        if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
            arrSource = unescape(this.location.search).substring(1,this.location.search.length).split("&"), i = 0;
            while(i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++;
        }
        return paramValue == "" && (paramValue = null) , paramValue
    }

    function reloadDom() {
        $("#title").text(itemVO.title);
        $("#description").text(itemVO.description);
        $("#price").text(itemVO.price);
        $("#imgUrl").attr("src",itemVO.imgUrl);
        $("#stock").text(itemVO.stock);
        $("#sales").text(itemVO.sales);
        if (itemVO.promoStatus == 1) {
            // 活动还未开始
            // 将2020-07-15T00:00:00 的T去掉，并且将-换为/
            var startTime = itemVO.startTime.replace("T"," ").replace(new RegExp("-","gm"),"/");
            // 将日期时间转为毫秒数
            startTime = (new Date(startTime)).getTime();
            var nowTime = Date.parse(new Date());
            var delta = (startTime - nowTime) / 1000;
            if (delta <= 0) {
                // 活动开始了
                itemVO.promoStatus = 2;
                reloadDom();
            }
            $("#promoStartTime").text("活动将于：" + itemVO.startTime.replace("T"," ") + "开始！倒计时：" + delta);
            $("#promoPrice").text(itemVO.promoPrice);
            $("#createOrder").attr("disabled",true);
        } else if (itemVO.promoStatus == 2) {
            // 活动正在进行中
            $("#promoStartTime").text("活动正在进行中");
            $("#promoPrice").text(itemVO.promoPrice);
            $("#priceContainer").hide();
            $("#createOrder").attr("disabled",false);
        } else {
            $("#promoStartTimeContainer").hide();
            $("#promoPriceContainer").hide();
        }
    }
</script>

</html>